package com.friend.base.controller.base;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.friend.base.service.base.CircleService;
import com.friend.base.service.base.DictService;
import com.friend.base.service.base.MemberCircleService;
import com.friend.common.model.BaseController;
import com.friend.common.model.Result;
import com.friend.entity.entity.base.Circle;
import com.friend.entity.entity.base.MemberCircle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author friend
 * @since 2019-10-17
 */
@Api(tags = {"活动圈前台控制器"})
@Slf4j
@RestController
@RequestMapping(value = "/circle" , produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class CircleController extends BaseController {

    @Autowired
    private CircleService circleService;
    @Autowired
    private DictService dictService;
    @Autowired
    private MemberCircleService memberCircleService;

    @GetMapping("/getMemCircle")
    @ApiOperation(value = "获取个人爱好的圈子", notes = "根据会员主键获取个人爱好的圈子", httpMethod = "GET")
    public Result getMemCircle(@RequestParam(value = "honorsStr", required = false) String honorsStr, @RequestParam("memId") Integer memId){
        try {
            Collection<Circle> circles = new ArrayList<>();
            List<MemberCircle> memberCircles = memberCircleService.list(new QueryWrapper<MemberCircle>().select("circleId").eq("memId", memId));
            List<Integer> circleIds = memberCircles.stream().map(relate -> relate.getCircleId()).collect(Collectors.toList());
            if (StringUtils.isNotBlank(honorsStr)){
                String[] honors = honorsStr.split(",");
                circles = circleService.list(new QueryWrapper<Circle>().in("type", honors)
                        .orderByDesc("create_time"));
            }else{
                circles = circleService.list(new QueryWrapper<Circle>().orderByDesc("create_time"));
            }
            circles.forEach(circle -> {
                circle.setPic(dictService.getById(circle.getPic()).getValue());
                if (CollectionUtil.isNotEmpty(circleIds) && circleIds.contains(circle.getId())){
                    circle.setJoinFlag(1);
                }else {
                    circle.setJoinFlag(0);
                }
            });
            return this.createSuccessResult(circles);
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("圈子列表查询失败");
        }
    }

    @GetMapping("/getOtherMemCircle")
    @ApiOperation(value = "获取其他个人爱好的圈子", notes = "根据会员主键获取其他个人爱好的圈子", httpMethod = "GET")
    public Result getOtherMemCircle(@RequestParam("memId") Integer memId, @RequestParam("myId") Integer myId){
        try {
            List<MemberCircle> memberCircles = memberCircleService.list(new QueryWrapper<MemberCircle>().select("circleId").eq("memId", memId));
            List<MemberCircle> myMemberCircles = memberCircleService.list(new QueryWrapper<MemberCircle>().select("circleId").eq("memId", myId));
            List<Integer> joinCircles = myMemberCircles.stream().map(memberCircle -> memberCircle.getCircleId()).collect(Collectors.toList());
            List<Integer> circleIds = memberCircles.stream().map(circle -> circle.getCircleId()).collect(Collectors.toList());
            Collection<Circle> circles = circleService.list(Wrappers.<Circle>lambdaQuery().in(Circle::getId, circleIds));
            circles.forEach(circle -> {
                if (joinCircles.contains(circle.getId())){
                    circle.setJoinFlag(1);
                }else {
                    circle.setJoinFlag(0);
                }
            });
            return this.createSuccessResult(circles);
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("其他圈子列表查询失败");
        }
    }

    @PostMapping("/saveCircle")
    @ApiOperation(value = "保存圈子", notes = "保存会员创建的圈子", httpMethod = "POST")
    public Result saveCircle(@RequestBody Circle circle){
        try {
            circle.setUpdateUser(circle.getCreateUser());
            boolean flag = circleService.save(circle);
            if (flag){
                log.info("保存圈子,保存时间date-{},圈子信息-{}",LocalDate.now(),circle);
                return this.createSuccessResult(flag);
            }
            return this.createFailedResult("保存圈子失败");
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("保存圈子失败");
        }
    }
}

